import Taro from "@tarojs/taro";

import config from './../config/domian'
import allExtrance from './../config/allExtrance'
import Public from './public'

const PhoneLogin = {
    phoneLogin(json) {
        let url = config[process.env.NODE_ENV].hostSaas + '/login'
        // let domain = config[process.env.NODE_ENV].domain
        let header
        if (Taro.getEnv() === Taro.ENV_TYPE.WEB) {
            header = {
                domain: config[process.env.NODE_ENV].domain,
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        } else {
            header = {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }
        return new Promise((resolve, reject) => {
            Taro.request({
                url: url,
                data: json,
                method: 'POST',
                header: header
            })
                .then(res => {
                    if (res.data.status == 1) {
                        allExtrance.util.Taro.setStorageSync('Authorization', res.data.data.authorization)
                        if(Taro.getEnv() === Taro.ENV_TYPE.WEAPP){
                            allExtrance.util.Taro.setStorageSync('hasWechatUser', res.data.data.hasWechatUser)
                        }else if(Taro.getEnv() === Taro.ENV_TYPE.WEB){
                            allExtrance.util.Taro.setStorageSync('hasWechatUser', res.data.data.hasH5WechatUser)
                        }
                        allExtrance.util.Taro.setStorageSync('user_saas_id', res.data.data.user_saas_id)
                        allExtrance.util.Taro.setStorageSync('phone', res.data.data.phone)
                        Taro.showToast({
                            title: '登陆成功',
                            icon: 'success',
                        })
                            .then(res => {
                                Public.Person().then((result) => {
                                    resolve(true)
                                    Taro.hideLoading()
                                })
                                // Taro.reLaunch({
                                //     url: '/pages/consumer/index/index'
                                // })
                            })
                    } else {
                        reject(res.data.info)
                    }
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    userInfo(json) {
        let url = config[process.env.NODE_ENV].hostSaas + '/login/wechatMPAuthorize'
        return new Promise((resolve, reject) => {
            Taro.request({
                url: url,
                data: json,
                method: 'POST',
                header: {
                    Authorization: Taro.getStorageSync('Authorization'),
                    'Content-Type': 'application/x-www-form-urlencoded',
                }
            }).then(res => {
                if (res.data.status == 1) {
                    Public.Person().then((result) => {
                        resolve(true)
                    })
                } else {
                    reject(res.data.info)
                }
            })
        })
    },
    webEmpower(json) {
        let url = 'https://job.banchengyun.com/wechat/webEmpower/empower'
        let domain = config[process.env.NODE_ENV].domain
        window.location.href = `${url}?domain=${domain}&redirectUrl=${encodeURIComponent(config[process.env.NODE_ENV].hostSaas + '/#' + json.url)}&Authorization=${Taro.getStorageSync('Authorization')}`
        allExtrance.util.Taro.setStorageSync('hasWechatUser', true)
    },
}

export default PhoneLogin;

